package com.zhenhao.xsq.ui.activity.home;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.application.AppConfig;
import com.zhenhao.xsq.base.BaseFragment;
import com.zhenhao.xsq.entity.EvenbugMessage;
import com.zhenhao.xsq.ui.activity.home.component.DaggerMineComponent;
import com.zhenhao.xsq.ui.activity.home.contract.MineContract;
import com.zhenhao.xsq.ui.activity.home.module.MineModule;
import com.zhenhao.xsq.ui.activity.home.presenter.MinePresenter;
import com.zhenhao.xsq.ui.activity.user.UserCardActivity;
import com.zhenhao.xsq.ui.activity.user.UserListActivity;
import com.zhenhao.xsq.ui.activity.user.UserManagerActivity;
import com.zhenhao.xsq.utils.SpSaveUtils;
import com.zhenhao.xsq.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author xlei
 * @Package com.cxwl.xlxgj.ui.activity.home
 * @Description: $description
 * @date 2019/01/15 10:41:56
 */

public class MineFragment extends BaseFragment implements MineContract.View {

    @Inject
    MinePresenter mPresenter;

    @BindView(R.id.tv_xm)
    TextView tvXm;
    @BindView(R.id.ll_user_manager)
    LinearLayout llUserManager;
    @BindView(R.id.ll_account_manager)
    LinearLayout llAccountManager;
    @BindView(R.id.ll_user_card)
    LinearLayout llUserCard;
    @BindView(R.id.ll_about_us)
    LinearLayout llAboutUs;


    private View view;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_home_mine, null);
            ButterKnife.bind(this, view);
            Bundle mBundle = getArguments();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(UIUtils
                    .getDisplayWidth(getActivity()),
                    UIUtils.getStatusBarHeight(getActivity()) * 3);
            EventBus.getDefault().register(this);
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        initDataFromLocal();
    }

    @Override
    protected void setupFragmentComponent() {
        DaggerMineComponent
                .builder()
                .appComponent(((AppConfig) getActivity().getApplication())
                        .getApplicationComponent())
                .mineModule(new MineModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(MineContract.MineContractPresenter presenter) {
        mPresenter = (MinePresenter) presenter;
    }

    private boolean isload;

    @Override
    protected void initDataFromLocal() {
        Logger.e("MineFragment getUserVisibleHint() " + getUserVisibleHint() + " isUIVisible " + isUIVisible);

        // TODO: 2019/5/16 为什么这个isUIVisible永远都是false
//        if (!isViewCreated || !isUIVisible || isload) {


        if (!isViewCreated || isload) {
            return;
        }
        isload = true;
        loadData();
    }


    private void loadData() {
//        UserEntity.DataBean.YezhuVoBean yzInfoJson = SpSaveUtils.getYzInfoJson();
//        String yzInfoHead = SpSaveUtils.getYzInfoHead();
//        Log.e("人脸头像", "yzInfoHead " + yzInfoHead);
//        tvName.setText(yzInfoJson.getYezhuMc());
//        tvPhone.setText(yzInfoJson.getLianxiSj());
//        if (!TextUtils.isEmpty(yzInfoHead)) {
//
//            GlideUtils.loadAPIImgALL(AppConfig.getInstance(), yzInfoHead, RequestOptions
//                    .bitmapTransform(new CircleCrop()), ivUserHead);
//        } else {
//            Glide.with(AppConfig.getInstance())
//                    .load(R.mipmap.user_head)
//                    .into(ivUserHead);
//        }
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(EvenbugMessage message) {
        if (message.getType() == EvenbugMessage.MSG_HOME_REFRESH) {
            Log.e("刷新", "MineFragment 响应");
            loadData();
        }
    }

    @OnClick({R.id.ll_user_manager, R.id.ll_account_manager, R.id.ll_user_card, R.id.ll_about_us})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.ll_user_manager:
                startActivity(UserManagerActivity.class);
                break;
            case R.id.ll_account_manager:
                break;
            case R.id.ll_user_card:
//                startActivity(UserCardActivity.class);
                SpSaveUtils.putToolbarColor(0);
                startActivity(UserListActivity.class);
                break;
            case R.id.ll_about_us:
                break;
        }
    }
}